package vip.liux.backend.infrastructure.message;

import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.List;

public class MessageDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer {

    /**
     * Creates a new {@link MessageDataSourceScriptDatabaseInitializer} instance
     * using the specified data source and properties.
     *
     * @param dataSource the data source for message records
     * @param properties the properties for configuring the message record database
     */
    public MessageDataSourceScriptDatabaseInitializer(DataSource dataSource, MessageProperties properties) {
        this(dataSource, getSettings(dataSource, properties));
    }

    /**
     * Creates a new {@link DataSourceScriptDatabaseInitializer} that will initialize the
     * given {@code DataSource} using the provided settings.
     *
     * @param dataSource the data source to initialize
     * @param settings   the initialization settings
     */
    public MessageDataSourceScriptDatabaseInitializer(DataSource dataSource, DatabaseInitializationSettings settings) {
        super(dataSource, settings);
    }

    /**
     * Adapts {@link MessageProperties} to {@link DatabaseInitializationSettings},
     * replacing any {@literal @@platform@@} placeholders with the appropriate platform-specific values.
     *
     * @param dataSource the data source for message records
     * @param properties the properties for configuring the message record database
     * @return a new {@link DatabaseInitializationSettings} instance
     */
    static DatabaseInitializationSettings getSettings(DataSource dataSource, MessageProperties properties) {
        DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
        settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties));
        settings.setMode(properties.getInitializeSchema());
        settings.setContinueOnError(true);
        return settings;
    }

    /**
     * Resolves the schema locations for the given data source and properties, replacing
     * any {@literal @@platform@@} placeholders with the appropriate platform-specific values.
     *
     * @param dataSource the data source for message records
     * @param properties the properties for configuring the message record database
     * @return a list of resolved schema locations
     */
    private static List<String> resolveSchemaLocations(DataSource dataSource, MessageProperties properties) {
        PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
        if (StringUtils.hasText(properties.getPlatform())) {
            return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
        }
        return platformResolver.resolveAll(dataSource, properties.getSchema());
    }
}